1 /* 2 * The Apache Software License, Version 1.1 3 * 4 * Copyright (c) 2002 The Apache Software Foundation. All rights 5 * reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in 16 * the documentation and/or other materials provided with the 17 * distribution. 18 * 19 * 3. The end-user documentation included with the redistribution, 20 * if any, must include the following acknowledgment: 21 * "This product includes software developed by the 22 * Apache Software Foundation (http://www.apache.org/)." 23 * Alternately, this acknowledgment may appear in the software itself, 24 * if and wherever such third-party acknowledgments normally appear. 25 * 26 * 4. The names "Apache" and "Apache Software Foundation" must 27 * not be used to endorse or promote products derived from this 28 * software without prior written permission. For written 29 * permission, please contact apache@apache.org. 30 * 31 * 5. Products derived from this software may not be called "Apache", 32 * nor may "Apache" appear in their name, without prior written 33 * permission of the Apache Software Foundation. 34 * 35 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 36 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 37 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 38 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 41 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 42 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 43 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 44 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 45 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 46 * SUCH DAMAGE. 47 * ==================================================================== 48 * 49 * This software consists of voluntary contributions made by many 50 * individuals on behalf of the Apache Software Foundation. For more 51 * information on the Apache Software Foundation, please see 52 * <http://www.apache.org/>;. 53 */ 54 package net.sf.cglib.reflect; 55 56 import java.lang.reflect.Method; 57 import junit.framework.*; 58 59 /*** 60 * @version $Id: TestDelegates.java,v 1.2 2003/09/14 21:10:37 herbyderby Exp $ 61 */ 62 public class TestDelegates extends net.sf.cglib.CodeGenTestCase { 63 64 public interface StringMaker { 65 Object newInstance(char[] buf, int offset, int count); 66 } 67 68 public void testConstructor() throws Throwable { 69 StringMaker maker = (StringMaker)ConstructorDelegate.create(String.class, StringMaker.class); 70 assertTrue("nil".equals(maker.newInstance("vanilla".toCharArray(), 2, 3))); 71 } 72 73 public interface Substring { 74 String substring(int start, int end); 75 } 76 77 public interface Substring2 { 78 Object anyNameAllowed(int start, int end); 79 } 80 81 public interface IndexOf { 82 int indexOf(String str, int fromIndex); 83 } 84 85 public void testFancy() throws Throwable { 86 Substring delegate = (Substring)MethodDelegate.create("CGLIB", "substring", Substring.class); 87 assertTrue("LI".equals(delegate.substring(2, 4))); 88 } 89 90 public void testFancyNames() throws Throwable { 91 Substring2 delegate = (Substring2)MethodDelegate.create("CGLIB", "substring", Substring2.class); 92 assertTrue("LI".equals(delegate.anyNameAllowed(2, 4))); 93 } 94 95 public void testFancyTypes() throws Throwable { 96 String test = "abcabcabc"; 97 IndexOf delegate = (IndexOf)MethodDelegate.create(test, "indexOf", IndexOf.class); 98 assertTrue(delegate.indexOf("ab", 1) == test.indexOf("ab", 1)); 99 } 100 101 public void testEquals() throws Throwable { 102 String test = "abc"; 103 MethodDelegate mc1 = MethodDelegate.create(test, "indexOf", IndexOf.class); 104 MethodDelegate mc2 = MethodDelegate.create(test, "indexOf", IndexOf.class); 105 MethodDelegate mc3 = MethodDelegate.create("other", "indexOf", IndexOf.class); 106 MethodDelegate mc4 = MethodDelegate.create(test, "substring", Substring.class); 107 MethodDelegate mc5 = MethodDelegate.create(test, "substring", Substring2.class); 108 assertTrue(mc1.equals(mc2)); 109 assertTrue(!mc1.equals(mc3)); 110 assertTrue(!mc1.equals(mc4)); 111 assertTrue(mc4.equals(mc5)); 112 } 113 114 public static interface MainDelegate { 115 int main(String[] args); 116 } 117 118 public static class MainTest { 119 public static int alternateMain(String[] args) { 120 return 7; 121 } 122 } 123 124 public void testStaticDelegate() throws Throwable { 125 MainDelegate start = (MainDelegate)MethodDelegate.createStatic(MainTest.class, 126 "alternateMain", 127 MainDelegate.class); 128 assertTrue(start.main(null) == 7); 129 } 130 131 public static interface Listener { 132 public void onEvent(); 133 } 134 135 public static class Publisher { 136 public int test = 0; 137 private MulticastDelegate event = MulticastDelegate.create(Listener.class); 138 public void addListener(Listener listener) { 139 event = event.add(listener); 140 } 141 public void removeListener(Listener listener) { 142 event = event.remove(listener); 143 } 144 public void fireEvent() { 145 ((Listener)event).onEvent(); 146 } 147 } 148 149 public void testPublisher() throws Throwable { 150 final Publisher p = new Publisher(); 151 Listener l1 = new Listener() { 152 public void onEvent() { 153 p.test++; 154 } 155 }; 156 p.addListener(l1); 157 p.addListener(l1); 158 p.fireEvent(); 159 assertTrue(p.test == 2); 160 p.removeListener(l1); 161 p.fireEvent(); 162 assertTrue(p.test == 3); 163 } 164 165 public static interface SuperSimple { 166 public int execute(); 167 } 168 169 public void testMulticastReturnValue() throws Throwable { 170 SuperSimple ss1 = new SuperSimple() { 171 public int execute() { 172 return 1; 173 } 174 }; 175 SuperSimple ss2 = new SuperSimple() { 176 public int execute() { 177 return 2; 178 } 179 }; 180 MulticastDelegate multi = MulticastDelegate.create(SuperSimple.class); 181 multi = multi.add(ss1); 182 multi = multi.add(ss2); 183 assertTrue(((SuperSimple)multi).execute() == 2); 184 multi = multi.remove(ss1); 185 multi = multi.add(ss1); 186 assertTrue(((SuperSimple)multi).execute() == 1); 187 } 188 189 public TestDelegates(String testName) { 190 super(testName); 191 } 192 193 public static void main(String[] args) { 194 junit.textui.TestRunner.run(suite()); 195 } 196 197 public static Test suite() { 198 return new TestSuite(TestDelegates.class); 199 } 200 201 }

This page was automatically generated by Maven